home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / soundexample / soundexample.c < prev   
C/C++ Source or Header  |  1992-05-06  |  6KB  |  179 lines

  1. /*
  2.  * manx.aztec/amiga #530, from jgoodnow, 4568 chars, Fri Feb  6 02:48:41 1987
  3.  *
  4.  * Well, here is the double buffered sound example working with 16 bit
  5.  * ints, small code and small data. Note the Disable/Enable bracketing
  6.  * around the BeginIO() call in the interrupt routine. Without them,
  7.  * it crashes pretty reliably. Took a while to find that....
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <exec/interrupts.h>
  13. #include <exec/errors.h>
  14. #include <hardware/custom.h>
  15. #include <libraries/dos.h>
  16. #include <devices/audio.h>
  17.  
  18. #define BUFSIZE 250L
  19. #define STP2    10
  20. #define STSIZE  (1<<STP2)
  21. #define STSTEP  (2*3.141593/STSIZE)
  22. #define XMOUSEREG       (*((BYTE *)0xdff00b))
  23. #define YMOUSEREG       (-(*(BYTE *)0xdff00a))
  24.  
  25. extern struct MsgPort *CreatePort();
  26. extern struct Library *OpenLibrary();
  27. extern void *AllocMem();
  28. extern struct IOAudio *GetMsg();
  29. UBYTE allocationMap[]={1,8,2,4};
  30. struct Library *MathBase=0;
  31. struct MsgPort *allocPort=0;
  32. struct IOAudio *allocIOB=0;
  33. struct Device *device=0;
  34. struct Interrupt *interrupt=0;
  35. struct MsgPort *soundPort=0;
  36. BYTE *buffer[2]={0};
  37. struct IOAudio *soundIOB[2]={0};
  38.  
  39. int newBuffer();
  40. UBYTE sineTable[STSIZE];
  41. ULONG angle=0;
  42. ULONG frequency=0x2000000;
  43. BYTE lastYMouse;
  44.  
  45. main()
  46. {
  47.         int i;
  48.         FLOAT sine=0.0;
  49.         FLOAT cosine=1.0;
  50.  
  51.         if((MathBase=OpenLibrary("mathffp.library",0L))==0)
  52.                 cleanup("Cannot open math library");
  53.         for(i=0;i<STSIZE; ++i)  {
  54.                 sineTable[i]=127*sine+0.5;      
  55.                 sine += STSTEP * (cosine -= STSTEP * sine);
  56.         }
  57.  
  58.         lastYMouse=YMOUSEREG;
  59.  
  60.         if((allocPort=CreatePort("sound example",0L))==0)
  61.                 cleanup("Cannot create reply port");
  62.         if((allocIOB=AllocMem((long)sizeof(struct IOAudio),
  63.                                                                                         MEMF_PUBLIC|MEMF_CLEAR))==0)
  64.                 cleanup("Out of memory");
  65.         allocIOB->ioa_Request.io_Message.mn_Node.ln_Pri=-40;
  66.         allocIOB->ioa_Request.io_Message.mn_ReplyPort=allocPort;
  67.         allocIOB->ioa_Data=allocationMap;
  68.         allocIOB->ioa_Length=sizeof(allocationMap);
  69.  
  70.         switch(OpenDevice(AUDIONAME,0L,allocIOB,0L)) {
  71.         case IOERR_OPENFAIL:
  72.                 cleanup("Cannot open audio device");
  73.         case ADIOERR_ALLOCFAILED:
  74.                 cleanup("Cannot allocate audio channel");
  75.         }
  76.         device=allocIOB->ioa_Request.io_Device;
  77.  
  78.         if((interrupt=AllocMem((long)sizeof(struct Interrupt),
  79.                                                                                         MEMF_CLEAR|MEMF_PUBLIC))==0)
  80.                 cleanup("Out of memory");
  81.  
  82.         interrupt->is_Code=(VOID(*)())newBuffer;
  83.  
  84.  
  85.         if((soundPort=AllocMem((long)sizeof(struct MsgPort),
  86.                                                                                         MEMF_CLEAR|MEMF_CHIP))==0)
  87.                 cleanup("Out of memory");
  88.  
  89.         soundPort->mp_Flags=PA_SOFTINT;
  90.         soundPort->mp_SigTask=(struct Task *)interrupt;
  91.         soundPort->mp_Node.ln_Type=NT_MSGPORT;
  92.         NewList(&soundPort->mp_MsgList);
  93.  
  94.  
  95.         for (i=0; i<2; ++i) {
  96.                 if((buffer[i]=AllocMem(BUFSIZE,MEMF_CHIP))==0)
  97.                         cleanup("Out of Memory");
  98.                 if((soundIOB[i]=AllocMem((LONG) sizeof(struct IOAudio),
  99.                                                                                         MEMF_PUBLIC|MEMF_CLEAR))==0)
  100.                         cleanup("Out of memory");
  101.                 soundIOB[i]->ioa_Request.io_Message.mn_ReplyPort=soundPort;
  102.                 soundIOB[i]->ioa_Request.io_Device=device;
  103.                 soundIOB[i]->ioa_Request.io_Unit=allocIOB->ioa_Request.io_Unit;
  104.                 soundIOB[i]->ioa_Request.io_Command=CMD_WRITE;
  105.                 soundIOB[i]->ioa_Request.io_Flags=ADIOF_PERVOL;
  106.                 soundIOB[i]->ioa_AllocKey=allocIOB->ioa_AllocKey;
  107.                 soundIOB[i]->ioa_Data=(UBYTE *)buffer[i];
  108.                 soundIOB[i]->ioa_Length=BUFSIZE;
  109.                 soundIOB[i]->ioa_Period=200;
  110.                 soundIOB[i]->ioa_Volume=64;
  111.                 soundIOB[i]->ioa_Cycles=1;
  112.                 ReplyMsg(soundIOB[i]);
  113.         }
  114.         printf("Press ctrl-c to stop\n");
  115.         Wait(SIGBREAKF_CTRL_C);
  116.         cleanup("");
  117. }  /* end main */
  118.  
  119. /************************************/
  120. cleanup(mes)
  121. TEXT *mes;
  122. {
  123.         int i;
  124.         printf(mes);
  125.         printf("\n");
  126.         if(device!=0)
  127.                 CloseDevice(allocIOB);
  128.         for(i=0; i<2; ++i) {
  129.                 if(soundIOB[i])
  130.                         FreeMem(soundIOB[i],(LONG)sizeof(struct IOAudio));
  131.                 if(buffer[i])
  132.                         FreeMem(buffer[i],BUFSIZE);
  133.         }
  134.         if(soundPort)
  135.                 FreeMem(soundPort,(LONG)sizeof(struct MsgPort));
  136.         if(interrupt)
  137.                 FreeMem(interrupt,(LONG) sizeof(struct Interrupt));
  138.         if(allocIOB)
  139.                 FreeMem(allocIOB,(LONG)sizeof(struct IOAudio));
  140.         if(allocPort)
  141.                 DeletePort(allocPort,(LONG)sizeof(struct MsgPort));
  142.         if (MathBase)
  143.                 CloseLibrary(MathBase);
  144.         exit(0);
  145. }
  146.  
  147. newBuffer()
  148. {
  149.         int i;
  150.         struct IOAudio *ioa;
  151.         BYTE *buffer;
  152.         BYTE mouseChange,curYMouse;
  153.         ULONG newFreq;
  154. #asm
  155.         movem.l d2/d3/a4,-(sp)
  156. #endasm
  157.         geta4();
  158.  
  159.         ioa=GetMsg(soundPort);
  160.         if (ioa && ioa->ioa_Request.io_Error==0) {
  161.                 curYMouse=YMOUSEREG;
  162.                 mouseChange=curYMouse-lastYMouse;
  163.                 lastYMouse=curYMouse;
  164.                 newFreq=frequency+mouseChange*(frequency>>6);
  165.                 if(newFreq>0x800000 && newFreq <0x40000000)
  166.                         frequency=newFreq;
  167.                 buffer=(BYTE *)ioa->ioa_Data;
  168.                 for(i=0; i<BUFSIZE; ++i)
  169.                         *buffer++=sineTable[(angle+=frequency)>>(32-STP2)];
  170.                 Disable();
  171.                 BeginIO(ioa);
  172.                 Enable();
  173.         }
  174.         ;
  175. #asm
  176.         movem.l (sp)+,d2/d3/a4
  177. #endasm 
  178. }
  179.